package com.apk.tools;


import java_util.coo_util.C_File;
import java_util.coo_util.C_String;
import java_util.coo_util.Log.C_Log;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class build_config {

    public static String _build = "_build";
    public final static boolean o_isDelectTempFile = false;

    public String o_packageName = "x";
    public String o_gameName = null;
    public String o_lastPackageName = "x";
    public String o_keystoreName = "sign.keystore";
    public String o_keystorePass = "123456";
    public String o_keyaliasName = "tejie";
    public String o_keyaliasPass = "123456";
    Map<String, String> configMap = new HashMap();

    public boolean S_LoadData(String currPath) throws Exception {
        String fileName = currPath + "build_config.txt";
        C_Log.Log(fileName);

        List<String> list = C_File.ReadAllLines(fileName);
        if (list == null) {
            C_Log.LogError("build.build_config.......不存在无法打包....." + fileName);
            return false;
        }
        for (String ss : list) {
            if (ss.length() < 2) {
                continue;
            }
            String[] sss = C_String.string0_split(ss, "[= ;]", true);
            if (sss.length < 2) {
                continue;
            }
            String key = sss[0].trim();
            key = S_ClearStringHead(key);
            String value = sss[1].trim();
            for (int v = 0; v < key.length(); v++) {
                byte[] cc = key.getBytes();
            }
            C_Log.Log(key + "__________" + value);
            if (key.equals("packageName") && value.length() > 1) {
                o_packageName = value;
            } else if (key.equals("gameName") && value.length() >= 1) {
                o_gameName = value;
            } else if (key.equals("keystoreName") && value.length() >= 1) {
                o_keystoreName = value;
            } else if (key.equals("keystorePass") && value.length() > 1) {
                o_keystorePass = value;
            } else if (key.equals("keyaliasName") && value.length() > 1) {
                o_keyaliasName = value;
            } else if (key.equals("keyaliasPass") && value.length() > 1) {
                o_keyaliasPass = value;
            } else {
                configMap.put(key, value);
            }
        }
        return true;
    }

    public String temp_buildId, temp_package, temp_userId, temp_wordPath;
    public int temp_buildType;

    public void S_SetInfo(File wordDir, String buildId, int buildType) throws IOException {
        temp_wordPath = wordDir.getCanonicalPath();
        temp_userId = new File(wordDir.getParent()).getName();
        temp_package = wordDir.getName();
        temp_buildId = buildId;
        temp_buildType = buildType;
        C_Log.LogFormat("当前打包信息.....%s....%s....%s", temp_userId, temp_package, temp_buildId);
    }

    static String S_ClearStringHead(String str) {
        int length = str.length();
        char[] chars = new char[length];
        str.getChars(0, length, chars, 0);
        int errorPosi = -1;
        for (int v = 0; v < chars.length; v++) {
            if (chars[v] != 35 && (chars[v] < 48 || chars[v] > 122)) {
                //System.out.println("错误......" + chars[v]);
                errorPosi = v;
            } else {
                //System.out.println("数据......................." + chars[v]);
                break;
            }
        }
        if (errorPosi < 0) {
            return str;
        }
        return str.substring(errorPosi + 1);
    }


    public static void S_FixConfig(String path, build_config setting) throws IOException {
//        try {

        StringBuilder data = new StringBuilder();
        List<String> list = C_File.ReadAllLines(path);

        assert list != null;
        for (String ss : list) {
            if (ss.length() < 2) {
                continue;
            }
            String[] sss = ss.split("=");
            if (sss.length < 2) {
                continue;
            }
            String key = sss[0].trim();
            key = build_config.S_ClearStringHead(key);
            String value = sss[1].trim();
            if (!setting.configMap.containsKey(key)) {
                setting.configMap.put(key, value);
            }
        }
        for (Map.Entry<String, String> entry : setting.configMap.entrySet()) {
            C_Log.Log("S_FixConfig______" + entry.getKey() + "________________" + entry.getValue());
            data.append(entry.getKey()).append("=").append(entry.getValue()).append("\r\n");
        }
        C_File.S_Save(path, data.toString());
    }


    public static String S_SearchFilePath(String rootPath, String name) throws IOException {
        String apkPath = rootPath + name;
        if (!new File(apkPath).exists()) {
            apkPath = rootPath + "../" + name;
            File file = new File(apkPath);
            if (file.exists()) {
                apkPath = file.getCanonicalPath();
            } else {
                apkPath = rootPath + "../../" + name;
                file = new File(apkPath);
                apkPath = file.getCanonicalPath();
            }
        }
        return apkPath;
    }

}
